package tech.daoxuan.findresources.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsoupUtils {
    private static  String DEFAULT_ENCODING="utf8";
    static   Logger logger = LoggerFactory.getLogger(JsoupUtils.class);
    public static String getHtmlEncodingByRegex(String metas) throws IOException {
        String reg ="(?<=charset=)(.+)(?=\")";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(metas);
        if (m.find()){
            return StringUtils.isBlank(m.group())?DEFAULT_ENCODING:m.group();
        }
        return DEFAULT_ENCODING;
    }

    public static Element getHtmlBody(String strUrl) throws Exception {
        Document documentByUrl = getDocumentByUrl(strUrl);
        Elements metas = getMetas(documentByUrl);
        String htmlEncodingByRegex = getHtmlEncodingByRegex(metas.toString());
        DEFAULT_ENCODING=htmlEncodingByRegex;
         documentByUrl = getDocumentByUrl(strUrl);
        Element body = documentByUrl.body();
       // logger.info(body.toString());
        return body;
    }
    public static Elements getMetas(Document document){
        Element head = document.head();
        return head.select("head meta");
    }
    public static  Document getDocumentByUrl(String strUrl) throws IOException {
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpGet httpGet = new HttpGet(strUrl);
        //获取相应数据，这里可以获取相应的数据
        HttpResponse httpResponse = httpClient.execute(httpGet);
        //拿到实体
        HttpEntity httpEntity= httpResponse.getEntity();
        String  result = EntityUtils.toString(httpEntity,DEFAULT_ENCODING);
        httpGet.releaseConnection();
        return Jsoup.parse(result);
    }

}
